package Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

import Function.CheckIdCode;

public class InsertMember {
	boolean insert = false;
	public InsertMember(String Person_ID,String Name,String Lname,String Street,
			String City,String Province,String Zone,String Zipcode,String Telephone,
			String Email,String NoHome,String empID)
	{
		CheckMember checkNameLastName = new CheckMember(Name, Lname,Person_ID);
		CheckIdCode checkID = new CheckIdCode(Person_ID);
		if(checkNameLastName.Found() && checkID.Check()){
			try{
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				Date currentDate = new Date();
				String CreateDate = sdf.format(currentDate);
				sdf = new SimpleDateFormat("HH:mm:ss");
				Date currentTime = new Date();
				String CreateTime = sdf.format(currentTime);
				Class.forName("com.mysql.jdbc.Driver");
				Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/library_community","root","1234");
				String link = new String("INSERT INTO lcmm_member " +
					"(MEMBER_PERSON_ID,MEMBER_NAME,MEMBER_LNAME,MEMBER_STREET,MEMBER_CITY,MEMBER_PROVINCE,MEMBER_ZONE,"+
					"MEMBER_ZIPCODE,MEMBER_TELEPHONE,MEMBER_EMAIL,MEMBER_NO_HOME,MEMBER_CREATE_USER,"+
					"MEMBER_CREATE_DATE,MEMBER_CREATE_TIME,MEMBER_RENT_AMOUNT_BOOK)"+
					"values('" + Person_ID +"','" + Name + "','" + Lname + "','" + Street
					+ "','" + City + "','" + Province + "','" + Zone +"','"+ Zipcode
					+ "','" + Telephone + "','" + Email + "','" + NoHome+ "','" + empID + "','" 
					+ CreateDate + "','" + CreateTime + "','0')");
				Statement statement = con.createStatement();
				statement.executeUpdate(link);
				insert = true;
				statement.close();
				con.close();
			}catch (Exception e)
			{
				JOptionPane.showMessageDialog(null,e);
			}
		}
	}
	public boolean checkInsert()
	{
		return insert;
	}
}
